<?php
class XlsHelper extends AppHelper {

	var $rowsBuffer = ''; // buffer to keep rows in it

	function setHeader($filename = 'Spreadsheet') {
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");		
		header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
		header("Content-Type: application/force-download");
		header("Content-Type: application/download");;
		header("Content-Disposition: inline; filename=\"".$filename.".xls\"");
	}

	function getXmlHeader() {
		$header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		$header .= "<?mso-application progid=\"Excel.Sheet\"?>";
		$header .= "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n";		
		$header .= "		  xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n";
		$header .= "          xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n";
		$header .= "          xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n";
		$header .= "          xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n";
		$header .= "<Styles>";
		$header .= "<Style ss:ID=\"s21\">";
		$header .= "<Font ss:Bold=\"1\" />";
	    $header .= "</Style>";	    
		$header .= "<Style ss:ID=\"s63\">";
		$header .= "<Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Top\" />";
		$header .= "<Font ss:Bold=\"1\" />";
		$header .= "</Style>";		
		$header .= "<Style ss:ID=\"s64\">";
		$header .= "<Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Top\" />";
		$header .= "<Font ss:Bold=\"1\" />";
		$header .= "</Style>";
		$header .= "<Style ss:ID=\"s65\">";
		$header .= "<Alignment ss:Horizontal=\"Right\" ss:Vertical=\"Top\" />";
		$header .= "<Font ss:Bold=\"1\" />";
		$header .= "</Style>";
		$header .= "</Styles>";
		return $header;
	}

	function getWorkSheetName($workSheetName = 'Sheet1') {
		$sheetName = "\t<Worksheet ss:Name=\"".$workSheetName."\">\n";
		$sheetName .= "\t\t<Table>\n";
		return $sheetName;
	}

	function getXmlFooter() {
		$footer = "\t\t</Table>\n";
		$footer .= "\t</Worksheet>\n";
		$footer .= "</Workbook>\n";
		return $footer;
	}

	function openRow() {
		$this->rowsBuffer .= "\t\t\t<Row>\n";
		return;
	}

	function closeRow() {
		$this->rowsBuffer .= "\t\t\t</Row>\n";
		return;
	}
	
	function addCell($value, $bold) {	
		$properties = '';
		if($bold){
			$properties = 'ss:StyleID="s21"';		
		}
		
		$Value = $value;
		if(is_array($value)){
			
			$colspan = isset($Value['colspan']) ? $Value['colspan']-1:'';			
			$properties = !empty($colspan) ? 'ss:MergeAcross="'.$colspan.'" ' :'';
			$rowspan = isset($Value['rowspan']) ? $Value['rowspan']-1:'';			
			$properties .= !empty($rowspan) ? 'ss:MergeDown="'.$rowspan.'" ' :'';
			$properties .= isset($Value['align']) && $Value['align'] == 'left'? 'ss:StyleID="s64" ' :'';
			$properties .= isset($Value['align']) && $Value['align'] == 'right'? 'ss:StyleID="s65" ' :'';
			$properties .= isset($Value['align']) && $Value['align'] == 'center'? 'ss:StyleID="s63" ' :'';
									
			$Value = $value['celldata'];
		}
		
		
		
		if((is_numeric($Value) || is_int($Value)) && !is_null($Value)){
			$this->rowsBuffer .= "\t\t\t\t<Cell {$properties} ><Data ss:Type=\"Number\" >".$Value."</Data></Cell>\n";
		}else{
			$this->rowsBuffer .= "\t\t\t\t<Cell {$properties} ><Data ss:Type=\"String\" >".$Value."</Data></Cell>\n";
		}
		return;
		
	}
	 
	function addRow($rowData,$bold = false){
		$this->openRow();
		foreach($rowData as $cellData){
			$this->addCell($cellData,$bold);
		}
		$this->closeRow();
		return;
	}
	
	function render($fileName = 'MySpreadSheet', $worksheetName = 'Sheet1')
	{
		$xlsOutput = '';
		$this->setHeader($fileName);
		$xlsOutput = $this->getXmlHeader();
		$xlsOutput .= $this->getWorkSheetName($worksheetName);
		$xlsOutput .= $this->rowsBuffer;
		$xlsOutput .= $this->getXmlFooter();
		echo $xlsOutput;
		exit();
	}
}
?>